package com.mar.bt.server.impl;

import net.java.dev.marge.entity.Device;

public class BTL2CAPOutputStream extends BTOutputStream {

	BTL2CAPOutputStream(Device clientDEvice) {
		super(clientDEvice);
	}
	
	@Override
	protected void flushDataToBTDevice() {

		byte[] dataToSend = new byte[buffer.limit()];
		byte [] protocolSlice = new byte[BTProtocolConstants.L2CAP_TRANSMIT_MTU];
		
		// slices
		for ( int i = 0 ; i < buffer.limit() / BTProtocolConstants.L2CAP_TRANSMIT_MTU; i++ ){
			buffer.get( protocolSlice);
			btDevice.send(dataToSend);
		}
		// the tail
		byte [] tailBytes = new byte[buffer.limit() % BTProtocolConstants.L2CAP_TRANSMIT_MTU];
		if ( tailBytes.length > 0 ){
			buffer.get( tailBytes);
			btDevice.send(tailBytes);
		}
	}

}
